/**
 *  JFTSTool - Java Full Text Search tool.
 *  Copyright (C) <2009>  <grupo JFTS>
 *  This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jftstool.analyzers;

import java.io.Reader;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.jftstool.analyzers.exceptions.AnalyzerException;
import org.jftstool.analyzers.exceptions.FilterException;
import org.jftstool.analyzers.filtersFactory.IFilterFactory;
import org.jftstool.analyzers.tokenizers.ITokenizerFactory;
import org.jftstool.config.AnalyzerConfig;
import org.jftstool.config.FilterConfig;
import org.jftstool.config.JFTSConfiguration;
import org.jftstool.config.exceptions.JFTSConfigException;


/**
 * @author grupo JFTS
 *
 */
public abstract class JFTSAnalyzer extends Analyzer{
	protected AnalyzerConfig config = null;

	public JFTSAnalyzer(String name) throws AnalyzerException{
		try {
			JFTSConfiguration mainConfig = JFTSConfiguration.getInstance();
			config = mainConfig.getAnalyzer(name);
		}
		catch (JFTSConfigException e) {
			throw new AnalyzerException(e.getMessage());
		}
	}
	
	@Override
	public TokenStream tokenStream(String name, Reader r) {
		List<FilterConfig> filters = config.getFilters(); 
		String className = null;
	
		TokenStream result = null;
	
		try {
			//Load tokenizer class
			className = config.getTokenizerClass();
			Class<? extends ITokenizerFactory> tokenizerClass;
			tokenizerClass = Class.forName(className).asSubclass(ITokenizerFactory.class);
			ITokenizerFactory dinamicTokenizer = (ITokenizerFactory) tokenizerClass.newInstance();
			result = dinamicTokenizer.create(r, null);
			
			for(FilterConfig filter : filters){
				className = filter.getArg("class").trim();
				Class<? extends IFilterFactory> filterClass;
	
				filterClass = Class.forName(className).asSubclass(IFilterFactory.class);
				IFilterFactory dinamicFilter = (IFilterFactory) filterClass.newInstance();
				result = dinamicFilter.create(result, filter.getArgs());
			}
		}
		catch (ClassNotFoundException e) {
			System.err.println(e.getMessage());	
		}
		catch (InstantiationException e) {
			System.err.println(e.getMessage());
		} 
		catch (IllegalAccessException e) {
			System.err.println(e.getMessage());
		}
		catch (FilterException e) {
			System.err.println(e.getMessage());			
		}
		return result;		
	}
}